﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     FeedBack业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class FeedBackBLL : BaseBLL
    {
        private static FeedBackBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IFeedBackDao dao = null;

        private FeedBackBLL()
        {
            dao = FeedBackSqlMapDao.Instance;
        }

        public static FeedBackBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new FeedBackBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<FeedBack> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<FeedBack> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<FeedBack> GetListByParameters(object TopCounts, FeedBack feedback)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<FeedBack> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="FeedBackID"></param>
        /// <returns></returns>
        public FeedBack GetDetail(System.String feedbackid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(feedbackid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 新建
        /// 修改人：Wilson
        /// 修改时间：2011-12-14
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public void Add(FeedBack feedback)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;
            try
            {

                SqlMap.BeginTransaction();

                feedback.CreateDate = DateTime.Now;
                feedback.UpdateDate = DateTime.Now;
                feedback.BeginDate = DateTime.Now;
                feedback.EndDate = DateTime.Now;
                feedback.State = (int)Common.Enum.auvState.Enabled;
                feedback.IsDel = (int)Common.Enum.auvDel.NotDel;

                feedback.Reply = "";
                feedback.ReplyDate = DateTime.Now;

                dao.Add(feedback);

                ServiceInfo service = ServiceInfoBLL.Instance.GetDetail(feedback.ServiceID);
                if (service != null)
                {
                    if (feedback.IsGood)
                    {
                        service.GoodRating++;
                    }
                    else
                    {
                        service.BadRating++;
                    }
                    ServiceInfoBLL.Instance.UpdateServiceInfo(service);
                }
                SqlMap.CommitTransaction();

            }
            catch
            {
                SqlMap.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public int Update(FeedBack feedback)
        {
            try
            {
                ///daoManager.BeginTransaction();              

                return dao.Update(feedback);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##回复评论,并更新订单状态为已互评
        /// <summary>
        /// 回复评论,并更新订单状态为已互评
        /// 创建人：Wilson
        /// 创建时间：2011-11-24
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public int UpdateFeedBack(FeedBack feedback)
        {
            try
            {
                ///daoManager.BeginTransaction();
                //SqlMap.BeginTransaction();

                feedback.UpdateDate = DateTime.Now;
                int i = dao.Update(feedback);

                if (i > 0)
                {
                    AUV5.Model.OrderInfo orderInfo = AUV5.BLL.OrderInfoBLL.Instance.GetDetail(feedback.OrderID);

                    if (orderInfo != null)
                    {
                        orderInfo.StatusID = Convert.ToInt32(AUV5.Common.Enum.OrderState.HasMutualEvaluation);

                        i = AUV5.BLL.OrderInfoBLL.Instance.Update(orderInfo);
                    }
                }

                //SqlMap.CommitTransaction();

                return i;

                ///daoManager.CommitTransaction();
            }
            catch
            {
                //SqlMap.RollBackTransaction();
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="FeedBackID"></param>
        /// <returns></returns>
        public int Delete(System.String feedbackid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(feedbackid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public int Delete(FeedBack feedback)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(feedback);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##评论分页
        /// <summary>
        /// 评论分页
        /// 创建人：Wilson
        /// 创建时间：2011-10-30
        /// </summary>
        /// <param name="objParamters"></param>
        /// <returns></returns>
        public IList<FeedBack> GetPagedFeedBack(int pageindex, int pagesize, string sqlwhere, string orderby, ref int recordcount)
        {
            try
            {
                Model.NetPager pager = new Model.NetPager();
                pager.PageIndex = pageindex;
                pager.TableName = "FeedBack";
                pager.PrimaryKey = "FeedBackID";
                pager.Where = sqlwhere;
                pager.Order = orderby;
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.PageSize = pagesize;

                IList<FeedBack> list = dao.GetFeedBackPage(pager);
                recordcount = pager.TotalCount;
                return list;
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##通过服务ID得到多少人评论
        /// <summary>
        /// 通过服务ID得到多少人评论
        /// 创建人：Wilson
        /// 创建时间：2011-10-31
        /// </summary>
        /// <param name="serviceID"></param>
        /// <returns></returns>
        public int GetFeebBackDistinct(string serviceID)
        {
            try
            {
                return dao.GetFeebBackDistinct(serviceID);
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##通过服务ID得到评论数
        /// <summary>
        /// 通过服务ID得到评论数
        /// 创建人：Wilson
        /// 创建时间：2011-10-31
        /// </summary>
        /// <param name="servcieID"></param>
        /// <returns></returns>
        public int GetFeebBackCount(string servcieID)
        {
            try
            {
                return dao.GetFeebBackCount(servcieID);
            }
            catch
            {

                throw;
            }
        }
        #endregion

        #region##是否己对此订单做过评论
        /// <summary>
        /// 是否己对此订单做过评论
        /// 创建人：Wilson
        /// 创建时间：2011-11-24
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool IsComment(string userId, string orderId)
        {
            try
            {
                bool flag = false;
                Hashtable ht = new Hashtable();
                ht.Add("UserId", userId);
                ht.Add("OrderID", orderId);
                int count = dao.GetFeebBackCountByUserIdAndOrderId(ht);

                if (count > 0)
                {
                    flag = true;
                }

                return flag;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion


        #region##通过订单ID得到评论
        /// <summary>
        /// 通过订单ID得到评论
        /// 创建人：Wilson
        /// 创建时间：2011-11-24
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public FeedBack GetFeedBackByOrderId(string orderId)
        {
            try
            {
                return dao.GetFeedBackByOrderId(orderId);
            }
            catch
            {
                throw;
            }
        }
        #endregion

    }
}
